<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加菜单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__UI__/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="__UI__/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="__UI__/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div id="authTree"></div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="__UI__/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="__UI__/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="__UI__/js/common.js" charset="utf-8"></script>
<script>
    layui.use(['form','tree'], function () {
        var form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            $ = layui.$;

        //无刷新获取菜单数据
        function getAuth() {
            var data = [];
            $.ajax({
                type: 'post',
                url: '{:url("admin/group/getAuth")}',
                dataType: 'json',
                async: false, //同步执行,false:取消同步执行其他代码，不许请求结束后才执行其他代码
                success: function (res) {
                    data = res;
                }
            });
            return data;
        }

        //初始化树
        tree.render({
            elem: '#authTree'
            ,data: getAuth() //数据先留空
            ,id: 'authTree'
            ,showCheckbox: true  //是否显示复选框
            ,accordion: 0  //是否开启手风琴模式
            ,onlyIconControl: true //是否仅允许节点左侧图标控制展开收缩
            ,isJump: 0  //点击文案跳转地址
            ,edit: false  //操作节点图标
        });

        $.each('{$row.menu_ids}'.split(','),function (index,vo) {
            tree.setChecked('authTree',parseInt(vo));//parseInt('8'):将字符串的数字转换成int类型
        });



        //递归,获取选中的菜单编号
        function getNodeId(node,arrIds=[]){
            $.each(node,function (index,obj) {
                if(obj.children) getNodeId(obj.children,arrIds);//自己调用自己
                else arrIds.push(obj.id);//往数组中添加一个元素
            });
            return arrIds;
        }

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var node = tree.getChecked('authTree');
            postSubmit(
                $,
                "{:url('admin/group/auth')}",
                {
                    id: '{$row.id}',
                    ids: getNodeId(node).toString()
                },
                function (res) {
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                    parent.layui.table.reload('currentTableId');//刷新表格的数据
                },  //请求成功时的处理
                function () {} //请求错误时的处理
            );

            return false;
        });

    });
</script>
</body>
</html>